{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "name": "bug_report_model.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "display_name": "Python 3 (ipykernel)",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.9.7"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "rR4_BAUYs3Mb"
      },
      "source": [
        "![image.png]()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "i7XbLCXGkll9"
      },
      "source": [
        "# The Boring Model\n",
        "Replicate a bug you experience, using this model.\n",
        "\n",
        "[Remember! we're always available for support on Slack](https://www.pytorchlightning.ai/community)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "2LODD6w9ixlT"
      },
      "source": [
        "---\n",
        "## Setup env"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "zK7-Gg69kMnG"
      },
      "source": [
        "%%capture\n",
        "! pip install -qU pytorch-lightning"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "WvuSN5jEbY8P"
      },
      "source": [
        "---\n",
        "## Deps"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "w4_TYnt_keJi"
      },
      "source": [
        "import os\n",
        "\n",
        "import torch\n",
        "from torch.utils.data import DataLoader, Dataset\n",
        "\n",
        "from pytorch_lightning import LightningModule, Trainer"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "XrJDukwPtUnS"
      },
      "source": [
        "---\n",
        "## Data\n",
        "Random data is best for debugging. If you needs special tensor shapes or batch compositions or dataloaders, modify as needed"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hvgTiaZpkvwS"
      },
      "source": [
        "class RandomDataset(Dataset):\n",
        "    def __init__(self, size, num_samples):\n",
        "        self.len = num_samples\n",
        "        self.data = torch.randn(num_samples, size)\n",
        "\n",
        "    def __getitem__(self, index):\n",
        "        return self.data[index]\n",
        "\n",
        "    def __len__(self):\n",
        "        return self.len"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "sxVlWjGhl02D"
      },
      "source": [
        "num_samples = 10000"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "V7ELesz1kVQo"
      },
      "source": [
        "class BoringModel(LightningModule):\n",
        "    def __init__(self):\n",
        "        super().__init__()\n",
        "        self.layer = torch.nn.Linear(32, 2)\n",
        "\n",
        "    def forward(self, x):\n",
        "        return self.layer(x)\n",
        "\n",
        "    def training_step(self, batch, batch_idx):\n",
        "        loss = self(batch).sum()\n",
        "        self.log(\"train_loss\", loss)\n",
        "        return {\"loss\": loss}\n",
        "\n",
        "    def validation_step(self, batch, batch_idx):\n",
        "        loss = self(batch).sum()\n",
        "        self.log(\"valid_loss\", loss)\n",
        "\n",
        "    def test_step(self, batch, batch_idx):\n",
        "        loss = self(batch).sum()\n",
        "        self.log(\"test_loss\", loss)\n",
        "\n",
        "    def configure_optimizers(self):\n",
        "        return torch.optim.SGD(self.layer.parameters(), lr=0.1)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ubvW3LGSupmt"
      },
      "source": [
        "---\n",
        "## Define the test"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4Dk6Ykv8lI7X"
      },
      "source": [
        "def run():\n",
        "    train_data = DataLoader(RandomDataset(32, 64), batch_size=2)\n",
        "    val_data = DataLoader(RandomDataset(32, 64), batch_size=2)\n",
        "    test_data = DataLoader(RandomDataset(32, 64), batch_size=2)\n",
        "\n",
        "    model = BoringModel()\n",
        "    trainer = Trainer(\n",
        "        default_root_dir=os.getcwd(),\n",
        "        limit_train_batches=1,\n",
        "        limit_val_batches=1,\n",
        "        limit_test_batches=1,\n",
        "        num_sanity_val_steps=0,\n",
        "        max_epochs=1,\n",
        "        enable_model_summary=False,\n",
        "    )\n",
        "    trainer.fit(model, train_dataloaders=train_data, val_dataloaders=val_data)\n",
        "    trainer.test(model, dataloaders=test_data)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4dPfTZVgmgxz"
      },
      "source": [
        "---\n",
        "## Run Test"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "AAtq1hwSmjKe"
      },
      "source": [
        "run()"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Flyi--SpvsJN"
      },
      "source": [
        "---\n",
        "## Environment\n",
        "Run this to get the environment details"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0-yvGFRoaDSi"
      },
      "source": [
        "%%capture\n",
        "! wget https://raw.githubusercontent.com/Lightning-AI/lightning/master/requirements/collect_env_details.py"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "quj4LUDgmFvj"
      },
      "source": [
        "! python collect_env_details.py"
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}
